package client.service;

import java.io.DataInputStream;
import client.main.Client_main;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import client.StreamCloser;
import com.google.gson.Gson;

import model.Message;
import model.MsgType;

public class UpdateFriendListConnector {
	private Socket client;
    private DataOutputStream output;
    private DataInputStream input;
    private StreamCloser s;
	//private Message msg;
    
	public UpdateFriendListConnector(int useraccount) {
		try {
            client = new Socket(Client_main.ip, 12345);
            output = new DataOutputStream(client.getOutputStream());
            input = new DataInputStream(client.getInputStream());
            Message mes = new Message();
            Gson gson = new Gson();            
            
            mes.setUserID(useraccount);
            mes.setType(MsgType.FRIENDLIST_UPDATER);
            String json = gson.toJson(mes);
            output.writeUTF(json);
            
            json = input.readUTF();
			mes = gson.fromJson(json, Message.class);
			
			if(mes.getType() == MsgType.UPDATER_CONNECTED) {
				UpdateFriendList u = new UpdateFriendList(useraccount, input, output);
				 UpdateFriendListManager.addUpdateFriendList(useraccount, u);
				Thread t = new Thread(u);
				t.start();
				System.out.println("初始化UpdataFriendList");
			}                       
        } catch (IOException e) {
            System.out.println("连接服务器失败！");
            s.closeall(client,input,output);
            e.printStackTrace();
        }
	}
	
	
	public DataInputStream getInputStream() {
		return input;
	}
	
	public DataOutputStream getOutputStream() {
		return output;
	}
		
}

